/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.stats;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.Semaphore;

import nfse.NetFSE;

public class StatsUpdater extends Thread {
	
	private static final Semaphore available = new Semaphore(1, true);
    
	private String path = "";
    
    public StatsUpdater(String filePath) {
        this.path = filePath;
    }

    public void run() {
        try {
        	StatsUpdater.available.acquire();
        	
        	Properties props = new Properties();
            props.setProperty("user", NetFSE.STATS_DB_USER);
            props.setProperty("password", NetFSE.STATS_DB_PASSWORD);
            props.setProperty("client_encoding", "UTF8");

            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://" + NetFSE.STATS_DB_HOST + "/" + NetFSE.STATS_DB_DATABASE;

            Connection connection = DriverManager.getConnection(url, props);
            Statement statement = connection.createStatement();
        	
        	File f = new File(path);
        	if (!f.exists()) {
        		System.out.println("The specified file does not exist: '" + path + "'");
        		return;
        	}
        	statement.execute("BEGIN");
        	String line = null;
        	LineNumberReader in = new LineNumberReader(new FileReader(f));
        	Vector inserts = new Vector();
        	while ((line = in.readLine()) != null) {
        		//System.out.println("SQL: '" + line + "'");
        		
        		statement.execute(line);
        	}
        	in.close();
        	statement.execute("COMMIT");
        	
        	f.delete();
        	statement.close();
        	connection.close();
        	
        	System.out.println("Update finished at " + new Timestamp(System.currentTimeMillis()));
        	
        } catch (Exception e) {
            e.printStackTrace();
        }
        StatsUpdater.available.release();
    }
    
    

}